package org.book.dao;

import java.util.List;

import org.book.util.JDBC_Util;


public class BookDaoImp implements BookDao{

	@Override
	public boolean remove(int no) {
		String sql = "delete from book where book_id = ?";
		return JDBC_Util.superUpdate(sql, no)!=0;
	}

	@Override
	public boolean add(BookBean stu) {
		String sql = "insert into book(ISBN,bookName,price,press,author) values(?,?,?,?,?)";
		return JDBC_Util.superUpdate(sql, stu.getISBN(), stu.getBookName() ,stu.getPrice(), stu.getPress(), stu.getAuthor())!=0;
	}

	@Override
	public boolean isEmpty(int no) {
		String sql = "select count(*) from book where book_id = ?";
		return JDBC_Util.superSelect(sql, no)!=0;
	}

	@Override
	public boolean update(int no,BookBean stu) {
		String sql = "update book set ISBN = ?,bookName = ?,price = ?,press = ?,author = ? where book_id = ?";
		return JDBC_Util.superUpdate(sql, stu.getISBN(), stu.getBookName() ,stu.getPrice(), stu.getPress(), stu.getAuthor(),no)!=0;
	}

	

	@Override
	public BookBean getStuByNo(int no) {
		String sql = "select * from book where book_id = ?";
		return JDBC_Util.superSelect(BookBean.class, sql, no).get(0);
	}

	@Override
	public List<BookBean> getList(int pageCurrent, int pageSize) {
		String sql = "select * from book limit ?,?";
		return JDBC_Util.superSelect(BookBean.class, sql, (pageCurrent-1)*pageSize, pageSize);
	}
	@Override
	public int getCount() {
		String sql = "select count(*) from book";
		return JDBC_Util.superSelect(sql);
	}

	@Override
	public List<BookBean> getLike(String str) {
		String s = '%'+str+'%';
		String sql = "SELECT *\r\n" + 
				"FROM book\r\n" + 
				"WHERE book_id LIKE ?\r\n" + 
				"OR ISBN LIKE ?\r\n" + 
				"OR bookName LIKE ?\r\n" + 
				"OR price LIKE ?\r\n" + 
				"OR press LIKE ?\r\n" + 
				"OR author LIKE ?";
		return JDBC_Util.superSelect(BookBean.class, sql, s,s,s,s,s,s);
	}

	

}
